package org.wejde.muel;

import java.awt.Dimension;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;

/**
 * This class contains the JComponents and functions
 * related to the tabbed portion of the GUI.
 * 
 * The JComponents in this class are an array of fieldPanes
 * and JScrollPanels.
 * 
 * The methods include pass-through calls for retrieving
 * and clear/resets input fields from all the embedded
 * fieldPanes.
 * 
 * @author Eric Martin
 * @author Webb Pinner
 *
 */
public class tabPane extends JTabbedPane {

	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = 176821119342327083L;

	/**
	 * Array of fieldPanes.
	 */
	private fieldsPane[] tabs;
	
	/**
	 * Array of JScrollPanes.
	 */
	private JScrollPane[] tabScrolls;
	
	/**
	 * Array of Tab names.
	 */
	private String[] tabNames;
	
	/**
	 * Constructor method.
	 * 
	 * @param fieldsArray 2-dimensional array of field objects.
	 * @param fieldsPaneNames array of strings containing
	 * the names to use for the tabs.
	 */
	public tabPane(fields[][] fieldsArray, String fieldsPaneNames[]) {
		super();
		
		// DEBUG
		//System.err.println("Number of tabs: " + fieldsArray.length);
		
		tabs = new fieldsPane[fieldsArray.length];
		tabScrolls = new JScrollPane[fieldsArray.length];
		tabNames = fieldsPaneNames;
		
		this.setMinimumSize(new Dimension(570, 475));
		this.setSize(this.getMinimumSize());

		for(int i=0; i<fieldsArray.length; i++) {
			this.tabs[i] = new fieldsPane(fieldsArray[i]);
			this.tabScrolls[i] = new JScrollPane(tabs[i], ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			this.tabScrolls[i].setBorder(null);
			this.addTab(this.tabNames[i], this.tabScrolls[i]);
			
		}
	}
	
	/**
	 * Method to clear/reset all the fields in the visible
	 * tab.
	 */
	public void clearFields() {
		for(int i=0; i<this.tabs.length; i++) {
			if(this.tabs[i].isShowing()) {
				this.tabs[i].clearFields();
			}
		}
	}

	/**
	 * Method to retrieve all the fields in the visible
	 * tab.
	 */
	public String getFieldValues() {
		String msg = "";
		for(int i=0; i<this.tabs.length; i++) {
			if(this.tabs[i].isShowing()) {
				msg += this.tabs[i].getFieldValues();
			}
		}
		return msg;
	}
	
	/**
	 * Method to set the delimiter to use when collecting
	 * all of the values from the input fields.
	 * 
	 * @param delim string containing the delimiter.
	 */
	public void setDelimiter(String delim) {
		for(int i=0; i<this.tabs.length; i++) {
			this.tabs[i].setDelimiter(delim);
		}
	}
	
	/**
	 * Method to retrieve the delimiter used when
	 * collecting all values from the visable tab.
	 * 
	 * @return string containing the delimiter.
	 */
	public String getDelimiter() {
		return this.tabs[1].getDelimiter();
	}
}
